package parser

import (
	"GraduationProject/public/models"
	"GraduationProject/worker/neteasy_music/fetcher/static"
	"GraduationProject/worker/neteasy_music/neteasy_music_conf"
	"encoding/json"
	"regexp"
	"strconv"
	"strings"
)

//var songIDRegex = regexp.MustCompile(`<li><a href="[^=]*=([0-9]*)">([^<]*)</a></li>`)
var songIDRegex = regexp.MustCompile(`<a href="/song\?id=([0-9]*)">([^<]*)</a>`)

func Songs(seed models.Record, content []byte) (seeds []models.Record, records []models.Record) {
	var songName [][]models.Param
	subMatch := songIDRegex.FindAllStringSubmatch(string(content), -1)
	subMatchLen := len(subMatch)
	seeds, songName = make([]models.Record, subMatchLen), make([][]models.Param, subMatchLen)
	params, _ := json.Marshal([]models.Param{
		{Key: "limit", Value: "20"},
		{Key: "offset", Value: "0"},
	})
	for i, v := range subMatch {
		// v[1] songID
		// v[2] name
		seeds[i].SeedID = seed.SeedID
		url := strings.Replace(static.SongIDUrl, "{.songID}", v[1], 1)
		url = strings.Replace(url, "{.csrf}", "", 1)
		seeds[i].Url = url
		seeds[i].Params = string(params)
		seeds[i].FunctionName = neteasy_music_conf.NetEasyMusicComments

		// todo: 研究 elasticsearch 的查询方式
		songName[i] = []models.Param{
			{Key: "song_id", Value: v[1]},
			{Key: "name", Value: v[2]},
		}
	}

	uniqueKey, _ := json.Marshal(models.Param{
		Key:   "net_easy_music_songs_unique_" + strconv.Itoa(int(seed.ID)),
		Value: seed.Url,
	})
	songNameBytes, _ := json.Marshal(songName)
	records = []models.Record{{
		NotElastic:   true,
		SeedID:       seed.SeedID,
		Url:          seed.Url,
		Params:       seed.Params,
		FunctionName: seed.FunctionName,
		UniqueKey:    string(uniqueKey),
		Result:       string(songNameBytes),
		Remarks:      "playlist",
	}}

	return seeds, records
}

// <tr id="4678055151582272611145" class="even "><td class="left"><div class="hd "><span data-res-id="467805515" data-res-type="18" data-res-action="play" data-res-from="13" data-res-data="3063458322" class="ply ">&nbsp;</span><span class="num">1</span></div></td><td class=""><div class="f-cb"><div class="tt"><div class="ttc"><span class="txt"><a href="/song?id=467805515"><b title="Eleven&nbsp;Weeks">Eleven<div class="soil">n</div>&nbsp;Weeks</b></a></span></div></div></div></td><td class=" s-fc3"><span class="u-dur ">03:05</span><div class="opt hshow"><a class="u-icn u-icn-81 icn-add" href="javascript:;" title="添加到播放列表" hidefocus="true" data-res-type="18" data-res-id="467805515" data-res-action="addto" data-res-from="13" data-res-data="3063458322"></a><span data-res-id="467805515" data-res-type="18" data-res-action="fav" class="icn icn-fav" title="收藏"></span><span data-res-id="467805515" data-res-type="18" data-res-action="share" data-res-name="Eleven Weeks" data-res-author="Vansire" data-res-pic="http://p2.music.126.net/S3a7WuJCPl0OKYdANAk3VA==/19233756904732880.jpg" class="icn icn-share" title="分享">分享</span><span data-res-id="467805515" data-res-type="18" data-res-action="download" class="icn icn-dl" title="下载"></span></div></td><td class=""><div class="text" title="Vansire"><span title="Vansire"><a class="" href="/artist?id=12348095" hidefocus="true">Van<div class="soil">OC</div>sire</a></span></div></td><td class=""><div class="text"><a href="/album?id=35297265" title="The&nbsp;Rolling,&nbsp;Driftless&nbsp;North">Th<div class="soil">aJ8E</div>e&nbsp;Rolling,&nbsp;Driftless&nbsp;North</a></div></td></tr>

// <a href="/song\?id=([0-9]*)"><b[^>]*>([^<]*)<div[^>]*>[^>]*>([^<]*)</b>
